(function($){
	var __iphone5___ = navigator.userAgent.match(/(?:iPhone|iPod|iPad)\s*\D*\s*(\d)\.*/);
	var __appVer=window.navigator.appVersion;
	$.fn.maskInput = function(symbol){
		var symbol = !symbol;
		var quickExpr = symbol?/[0-9]|[\.\,]/:/[0-9]/,
		//check 0.[0-9]
		rfewtenths = /^0\.[0-9]$/,
		//check 0.
		rzerodecimal = /^0\.$/;
		__appVer = __appVer.toLowerCase();
		var idMaxLengthMap = {};
    var id = $(this).attr('id');
    var idMaxlength = $(this).attr('maxlength');
    if ((typeof id !== 'undefined') && (typeof idMaxlength !== 'undefined')) {
           idMaxLengthMap[id] = idMaxlength;
           //handle maxLength of android 4.1 above device
           if ( __appVer.indexOf("android 4.1") >= 0 ){ 
              $(this).removeAttr('maxlength');
           }
    }
   
		function listenKeyPress(e){
			e = e||window.event;
			var k = e.charCode||e.keyCode||e.which;
			var key = String.fromCharCode(k);
			var value = e.target.value;
			//handle maxLength of android 4.1 above device
			if ( __appVer.indexOf("android 4.1") >= 0 ){ 
				  var allowLength=idMaxLengthMap[e.target.id];
			  	if(value.length>=allowLength){
			  		e.preventDefault();
			  		return false;
			    }
			}
			if(!quickExpr.test(key)){
				e.preventDefault();
				return false;
			}
			if(value==""&&key==","){
				e.preventDefault();
				return false;
			}
			

			if(value==""&&key=="."){
				e.preventDefault();
				return false;
			}

			if(value=="0"){
				//The first "."
				if(key=="."&&!e.target.unrecognized){
					e.target.unrecognized = true;
					return true;
				}
				//"." already existed
				if(key=="."&&e.target.unrecognized){
					e.preventDefault();
					return false;
				}
				//[0-9]
				if(key!="."){
					//
					if(!symbol){
						return true;
					}
					if(e.target.unrecognized){
						e.target.unrecognized = null;
						return true;
					}
				}
			}
			//avoid input "." if "." has already existed
			if(value.indexOf(".")>-1&&key=="."){
				e.preventDefault();
				return false;
			}
			//avoid input "," if "," has already existed
			/* Allow the user input multiple ','
			if(value.indexOf(",")>-1&&key==","){
				e.preventDefault();
				return false;
			}
			*/
		}


		function listenKeyDown(e){
			e = e||window.event;
			var k = e.charCode||e.keyCode||e.which;
			if(k==13||k==10){
				e.preventDefault();
				return false;
			}
			var val = e.target.value;
			//if 0. when input or  0.[0-9] when you press backspace
			if(rzerodecimal.test(val) || (k==8&&rfewtenths.test(val))){
				//deem that the key will not be prevented in keypress phase
				e.target.unrecognized = true;
			}
			//trim out
			//cover IOS4
			if(k==8&&(val=="0"||val=="0.")){
				//clean up
				e.target.unrecognized = null;
			}
			//handle maxLength of number type
			if(__iphone5___&&__iphone5___[0]&&__iphone5___[1]&&__iphone5___[1] - 0 > 4){
				//get the type of input field
				//get the value of maxlength attribute
				var type = e.target.type,ml = e.target.maxLength;
				//if the length of value match the maxLength, and the input type is number
				if(type=="number"&& ml && val.length > (ml-1) && !e.target._maxlength){
					//set flag to identify, as the value of length will not be changed when pressing backspace
					e.target._maxlength = true;
					//backup the maxLength
					e.target.ml = ml;
				}
				if(e.target._maxlength){
					//backspace
					if(k==8){
						//werid case, the length of value will be changed if we have removed the commad
						//which auto formats by iOS5
						//assume the length of the value dose not change
						if(val.length == ml){
							e.target.ml = e.target.ml - 1;
						}else{
							//assume the length of the value has been changed
							//reset
							e.target._maxlength = false;
						}
						return true;
					}
					//compare the exactly length of input with the maxLength
					if(e.target.ml >= ml){
						//outnumber
						e.preventDefault();
						return false;
					}else{
						//in range, increase
						e.target.ml = e.target.ml + 1;
						return true;
					}
				}
			}
		}

		function isFilled(e){
			e = e||window.event;
			var val = e.target.value;
			if(val){
				var parentNode = e.target.parentNode;
				while(parentNode&&parentNode.nodeName!="LI"){
					parentNode = parentNode.parentNode;
				}
				if(parentNode&&parentNode.className&&parentNode.className.indexOf("filled")==-1){
					parentNode.className = parentNode.className + " filled";
				}
			}else if(val==''){
				//clear the placehodler
				//e.target.placeholder = '';
			}
		}

		function handlePalceHodler(e){
			e = e||window.event;
			var val = e.target.value;
			if(!val){
				e.target.value = '';
			}
		}

		$(this).bind("keypress",listenKeyPress)
		.bind("keydown",listenKeyDown)
		.bind("keyup",isFilled)
		.bind("focus",handlePalceHodler)
		.one("unmaskInput",function(){
			$(this).unbind("keypress",listenKeyPress)
			.unbind("keydown",listenKeyDown)
			.unbind("keyup",isFilled)
			.unbind("focus",handlePalceHodler);
		});
	}

	$.fn.unmaskInput=function() {
		return this.trigger('unmaskInput');
	};

})(jQuery);